package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Quality;
import com.ruoyi.system.mapper.QualityMapper;
import com.ruoyi.system.service.IQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 质量管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@Service
public class QualityServiceImpl implements IQualityService
{
    @Autowired
    private QualityMapper qualityMapper;

    /**
     * 查询质量管理
     *
     * @param qualityId 质量管理主键
     * @return 质量管理
     */
    @Override
    public Quality selectQualityByQualityId(Long qualityId)
    {
        return qualityMapper.selectQualityByQualityId(qualityId);
    }

    /**
     * 查询质量管理列表
     *
     * @param quality 质量管理
     * @return 质量管理
     */
    @Override
    public List<Quality> selectQualityList(Quality quality)
    {
        return qualityMapper.selectQualityList(quality);
    }

    /**
     * 新增质量管理
     *
     * @param quality 质量管理
     * @return 结果
     */
    @Override
    public int insertQuality(Quality quality)
    {
        return qualityMapper.insertQuality(quality);
    }

    /**
     * 修改质量管理
     *
     * @param quality 质量管理
     * @return 结果
     */
    @Override
    public int updateQuality(Quality quality)
    {
        return qualityMapper.updateQuality(quality);
    }

    /**
     * 批量删除质量管理
     *
     * @param qualityIds 需要删除的质量管理主键
     * @return 结果
     */
    @Override
    public int deleteQualityByQualityIds(Long[] qualityIds)
    {
        return qualityMapper.deleteQualityByQualityIds(qualityIds);
    }

    /**
     * 删除质量管理信息
     *
     * @param qualityId 质量管理主键
     * @return 结果
     */
    @Override
    public int deleteQualityByQualityId(Long qualityId)
    {
        return qualityMapper.deleteQualityByQualityId(qualityId);
    }

    @Override
    public String importQuality(List<Quality> qualitiesList, boolean updateSupport) {
        if (StringUtils.isNull(qualitiesList) || qualitiesList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Quality quality :qualitiesList)
        {
            try
            {
                // 验证是否存在这个用户
                this.insertQuality(quality);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、评估日期 ").append(quality.getAssessmentDate()).append(" 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、评估日期" + quality.getAssessmentDate() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
